﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace PreProcessingLanguage
{
    class Probability
    {
        Dictionary<string, int> listWord = new Dictionary<string, int>();
        Dictionary<string, int> listWordUS = new Dictionary<string, int>();
        Dictionary<string, Dictionary<string, double>> P = new Dictionary<string, Dictionary<string, double>>();
        public void readData(string pathW,string pathWUS)
        {
            StreamReader sr = new StreamReader(pathW);
            string s;
            while ((s = sr.ReadLine()) != null)
            {
                string[] kv = s.Split(':');
                listWord[kv[0]] = Int32.Parse(kv[1]);
            }
            sr.Close();
            sr = new StreamReader(pathWUS);
            while ((s = sr.ReadLine()) != null)
            {
                string[] kv = s.Split(':');
                listWordUS[kv[0]] = Int32.Parse(kv[1]);
            }
            sr.Close();
        }
        public void calculatingP()
        {
            P.Clear();
            string uS = "";
            foreach (KeyValuePair<string, int> pair in listWord)
            {
                uS = PreProcessing.ConvertToUnSign(pair.Key);
                if (!P.ContainsKey(uS))
                {
                    Dictionary<string, double> d = new Dictionary<string, double>();
                    d.Add(pair.Key, pair.Value*1.0 / listWordUS[uS]);
                    P.Add(uS, d);
                }
                else
                    P[uS].Add(pair.Key, pair.Value*1.0 / listWordUS[uS]);

            }
        }
        public void writeData(string path)
        {
            StreamWriter sw = new StreamWriter(path);
            string t;
            foreach (KeyValuePair<string, Dictionary<string,double>> pair in P)
            {
                t = pair.Key+":";
                foreach (KeyValuePair<string,double> pp in pair.Value)
                {
                    if (Math.Round(pp.Value, 3)!=0)
                        t += pp.Key + "-" + Math.Round(pp.Value,3)+";";
                }
                t=t.Remove(t.Length-1);
                sw.Write(t+"\n");
            }
            sw.Flush();
            sw.Close();
        }
        public void showData()
        {
            Console.WriteLine("List word:"+listWord.Count);
            Console.WriteLine("List word us:" + listWordUS.Count);
        }
    }
}
